package com.github.houbb.async.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class thread {

    private static ThreadPoolExecutor activMercRelRedisPool = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("111111");
            return thread;
        }
    }, new ThreadPoolExecutor.CallerRunsPolicy());

    public static void exec(Runnable task){
        activMercRelRedisPool.execute(task);
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("b");
        long start = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(list.size());
        for (String s : list){
            activMercRelRedisPool.execute(()->{
                test(s);
                latch.countDown();
            });
        }
        System.out.println("耗时："+(System.currentTimeMillis()-start));
    }

    private static void test(String s) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
